package com.foosun.util.wechat;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foosun.util.https.HttpClientConnectionManager;
  
/** 
 * 微信自定义菜单创建 
 */  
public class WxMenuUtils {  
    // http客户端  
    public static DefaultHttpClient httpclient;  
      
    static {  
        httpclient = new DefaultHttpClient();  
        httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient); // 接受任何证书的浏览器客户端  
    }  
  
    public static void main(String[] args) {  
        try {  
            // 获取accessToken -参数corpid，secret  
            String accessToken = WxCommonUtils.getAccessToken("wx9aa55a020a3241a5");  
            System.out.println(accessToken);             
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
      
      
    /** 
     * 创建菜单 
     */  
    public static String createMenu(String params, String accessToken) throws Exception {  
        HttpPost httpost = HttpClientConnectionManager.getPostMethod("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken);  
        httpost.setEntity(new StringEntity(params, "UTF-8"));  
        HttpResponse response = httpclient.execute(httpost);  
        String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");  
        System.out.println(jsonStr);  
        JSONObject object = JSON.parseObject(jsonStr);  
        return object.getString("errmsg");  
    }  
  
    /** 
     * 查询菜单 
     */  
    public static String getMenuInfo(String accessToken) throws Exception {  
        HttpGet get = HttpClientConnectionManager.getGetMethod("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken);  
        HttpResponse response = httpclient.execute(get);  
        String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");  
        return jsonStr;  
    }  
      
}  
